// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueRouter from 'vue-router'
import VueResource from 'vue-resource'
import './assets/css/style.less'
import index from './components/index'
import loading from './components/loading'
import toast from './components/toast'
import making from './components/making'
import MuseUI from 'muse-ui'
import 'muse-ui/dist/muse-ui.css'

Vue.use(MuseUI)
Vue.use(VueRouter)
Vue.use(VueResource)

const routes = [
    { path: '/index', name:'index' , component: index },
    { path: '/making', name:'making' , component: making },
    { path: '*', name:'index' , redirect: '/index' }
]

const router = new VueRouter({
    routes // （缩写）相当于 routes: routes
})

const app = new Vue({
    components: {
        loading,toast
    },
    data () {
    	return {
      		loadingShow:false,
            toast:{
                show:false,
                txt:""
            },
            /*topPopup:{
                show:false,
                txt:""
            },*/
            baseUrl:"http://nmb.zhixueyuan.net/",
            eventHub:new Vue()
    	}
    },
    methods:{
        showToast (txt) {
            this.toast = {
                show:true,
                txt:txt
            };
            if (this.toastTimer) clearTimeout(this.toastTimer);
            this.toastTimer = setTimeout(() => { this.toast.show = false }, 2000);
        },
        /*showTopPopup (txt,autoClose = true) {
            this.topPopup = {
                show:true,
                txt:txt
            };
            if (this.topPopupTimer) clearTimeout(this.topPopupTimer);
            if (autoClose) {
                this.topPopupTimer = setTimeout(() => { this.topPopup.show = false }, 2000);
            }
        },*/
        gotoLogin (path) {
            if (localStorage.nmb_token && localStorage.nmb_token!="") {
                this.$http.get(this.$root.baseUrl+'api.php/Users/nimiLoginToken/token/'+localStorage.nmb_token).then((response) => {
                    //this.showTopPopup("登录成功");
                    if (response.body.error == 0) {
                        localStorage.nmb_token = response.body.data.token;
                        if (path) {
                            this.$router.push({ name: path })
                        }
                        else{
                            this.$router.push({ name: sessionStorage.nmb_lastPath })
                        }
                    }
                    else{
                        this.$router.push({ name: 'index' })
                    }
                }, (response) => {
                    this.$root.showToast("登录失败");
                });
            }
            else{
                this.$router.push({ name: 'index' })
            }
        }
    },
    created () {
        
    },
    monuted () {
    },
    router,
}).$mount('#app')